version		:= $(shell . ./scripts/ptxdist_version.sh && echo $${PTXDIST_VERSION_FULL})
project		:= ptxdist-$(version)

prefix		:= @prefix@
exec_prefix	:= @exec_prefix@
abs_srcdir 	:= @abs_srcdir@
libdir	 	:= @libdir@
bindir	 	:= @bindir@
instdir		:= $(libdir)/$(project)

CC		:= @CC@
CXX		:= @CXX@
CFLAGS		:= @CFLAGS@
CXXFLAGS	:= @CXXFLAGS@
CPPFLAGS	:= @CPPFLAGS@
LDFLAGS		:= @LDFLAGS@

SHELL		:= @BASH@
RST2MAN		:= @RST2MAN@

export SHELL

@BUILD_NCONF_TRUE@NCONF = nconf

all: kconfig environment
	@touch .done

kconfig:
	@echo "building conf and mconf ..."
	@CC="$(CC)" \
	CXX="$(CXX)" \
	CFLAGS="$(CFLAGS)" \
	CXXFLAGS="$(CXXFLAGS)" \
	CPPFLAGS="$(CPPFLAGS)" \
	LDFLAGS="$(LDFLAGS)" \
	$(MAKE) -C "$(abs_srcdir)/scripts/kconfig" \
		CONF_LIBS="@CONF_LIBS@" \
		MCONF_LIBS="@MCONF_LIBS@" \
		NCONF_LIBS="@NCONF_LIBS@" \
		CURSES_LOC="@CURSES_LOC@" \
		conf mconf $(NCONF)
	@echo "done."

environment:
	@echo -n "preparing PTXdist environment ..."
	@ln -sf @AWK@ "$(abs_srcdir)/bin/awk"
	@ln -sf @GNU_chmod@ "$(abs_srcdir)/bin/chmod"
	@ln -sf @GNU_chown@ "$(abs_srcdir)/bin/chown"
	@ln -sf @GNU_mv@ "$(abs_srcdir)/bin/mv"
	@ln -sf @GNU_cp@ "$(abs_srcdir)/bin/cp"
	@ln -sf @GNU_rm@ "$(abs_srcdir)/bin/rm"
	@ln -sf @GNU_ln@ "$(abs_srcdir)/bin/ln"
	@ln -sf @GNU_rmdir@ "$(abs_srcdir)/bin/rmdir"
	@ln -sf @GNU_md5sum@ "$(abs_srcdir)/bin/md5sum"
	@ln -sf @GNU_mkdir@ "$(abs_srcdir)/bin/mkdir"
	@ln -sf @GNU_mktemp@ "$(abs_srcdir)/bin/mktemp"
	@ln -sf @GNU_install@ "$(abs_srcdir)/bin/install"
	@ln -sf @GNU_stat@ "$(abs_srcdir)/bin/stat"
	@ln -sf @GNU_mknod@ "$(abs_srcdir)/bin/mknod"
	@ln -sf @GNU_tty@ "$(abs_srcdir)/bin/tty"
	@ln -sf @GNU_tar@ "$(abs_srcdir)/bin/tar"
	@ln -sf @GNU_find@ "$(abs_srcdir)/bin/find"
	@ln -sf @PTXDIST_FILE@ "$(abs_srcdir)/bin/file"
	@ln -sf @GNU_cat@ "$(abs_srcdir)/bin/cat"
	@ln -sf @GNU_dirname@ "$(abs_srcdir)/bin/dirname"
	@ln -sf @GNU_readlink@ "$(abs_srcdir)/bin/readlink"
	@ln -sf @GNU_sort@ "$(abs_srcdir)/bin/sort"
	@ln -sf @PYTHON@ "$(abs_srcdir)/bin/python"
	@ln -sf @SED@ "$(abs_srcdir)/bin/sed"
	@ln -sf @GNU_xargs@ "$(abs_srcdir)/bin/xargs"
	@ln -sf @BASH@  "$(abs_srcdir)/bin/bash"
	@ln -sf @SYSCTL@ "$(abs_srcdir)/bin/sysctl"
	@ln -sf @MAKE@ "$(abs_srcdir)/bin/make"
	@if [ @SPHINX@ != "sphinx-build" ]; then \
		ln -sf @SPHINX@ "$(abs_srcdir)/bin/sphinx-build"; \
	fi
	@echo " done"

man: man/ptxdist.1.gz
man/ptxdist.1: doc/ptxdist.man doc/ref_parameter.inc
	@mkdir -pv man
	$(RST2MAN) $< $@

.INTERMEDIATE: man/ptxdist.1
man/ptxdist.1.gz: man/ptxdist.1
	gzip -f $<

clean:
	@rm -f .done
	@find "$(abs_srcdir)/bin" -type l -print0 | xargs -0 rm -f
	@$(MAKE) -C "$(abs_srcdir)/scripts/kconfig" clean

dirty-check:
	@case "$(version)" in \
		*-dirty) echo "refusing to install or package a dirty git tree!" ; exit 1 ;; \
	esac

install: all dirty-check
	@echo "installing PTXdist to $(DESTDIR)$(prefix)..."
	@rm -fr   "$(DESTDIR)$(instdir)"
	@mkdir -p "$(DESTDIR)$(instdir)"
	@tar -C "$(abs_srcdir)" -cf - \
		--exclude *~ \
		--exclude .*.sw* \
		--exclude .git \
		--exclude .gitignore \
		--exclude .pc \
		--exclude .svn \
		--exclude autom4te.cache \
		--exclude config.log \
		--exclude config.status \
		--exclude debian \
		--exclude state \
		. | \
		tar -o -C "$(DESTDIR)$(instdir)" -xvf -
	@if [ \! -e "$(DESTDIR)$(instdir)/.tarball-version" ]; then \
		echo -n "${version}" > "$(DESTDIR)$(instdir)/.tarball-version"; \
	fi
	@mkdir -p "$(DESTDIR)$(bindir)"
	@rm -f    "$(DESTDIR)$(bindir)/ptxdist"
	@ln -sf   "$(instdir)/bin/ptxdist" "$(DESTDIR)$(bindir)/ptxdist-$(version)"
	@ln -sf   "$(instdir)/bin/ptxdist-auto-version" "$(DESTDIR)$(bindir)/ptxdist"
	@if [ -d "$(DESTDIR)@BASH_COMPLETION_DIR@" -a \
		-w "$(DESTDIR)@BASH_COMPLETION_DIR@" ]; then \
		cp scripts/bash_completion "$(DESTDIR)@BASH_COMPLETION_DIR@/ptxdist"; \
	fi

dist: dirty-check
	@rm -rf "$(project)"
	@git archive "$(project)" --prefix="$(project)"/ > "${project}.tar"
	tar xf "${project}.tar"
	echo -n "${version}" > "${project}/.tarball-version"
	cd "$(project)" && ./autogen.sh && ./configure && make man

	tar --owner=0 --group=0 -rf "${project}.tar" \
		"${project}/man/ptxdist.1.gz" \
		"${project}/configure" \
		"${project}/.tarball-version"
	bzip2 "${project}.tar"
	md5sum "${project}.tar.bz2" > "${project}.tar.bz2.md5"

distclean: clean
	rm -fr Makefile
	rm -fr build-stamp
	rm -fr config.log config.status autom4te.cache

maintainer-clean: distclean
	rm -f configure

release:
	@scripts/make_$@.sh

